package com.newsee.remote.supplier.controller;


import com.newsee.remote.common.dto.supplier.srm.SrmDTO;
import com.newsee.remote.common.vo.supplier.SrmVO;
import com.newsee.remote.supplier.service.ICrmPurveycompanyService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 * 供应商前端控制器
 * </p>
 *
 * @author liangzhichen
 * @since 2019-07-31
 */
@Slf4j
@RestController
@RequestMapping("/crm/supplier/")
public class CrmPurveycompanyController {

    private final ICrmPurveycompanyService iCrmPurveycompanyService;

    public CrmPurveycompanyController(ICrmPurveycompanyService iCrmPurveycompanyService) {
        this.iCrmPurveycompanyService = iCrmPurveycompanyService;
    }

    @ApiOperation(value = "接收供应商主动推送接口")
    @PostMapping
    public SrmVO acceptSupplier(@RequestBody SrmDTO srmDTO) {
        log.info("供应商增量同步信息：" + srmDTO.toString());
        return iCrmPurveycompanyService.acceptSupplier(srmDTO);
    }

    public static void main(String[] args) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        ParsePosition pos1 = new ParsePosition(0);
        Date d1 = formatter.parse("2020-02-01", pos);
        Date d2 = formatter.parse("2020-04-02", pos1);

        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(d1);
        c2.setTime(d2);
        int year1 = c1.get(Calendar.YEAR);
        int year2 = c2.get(Calendar.YEAR);
        int month1 = c1.get(Calendar.MONTH);
        int month2 = c2.get(Calendar.MONTH);
        int day1 = c1.get(Calendar.DAY_OF_MONTH);
        int day2 = c2.get(Calendar.DAY_OF_MONTH);
        int yearInterval = year1 - year2;
        if (month1 < month2 || month1 == month2 && day1 < day2)
            yearInterval--;

        int monthInterval = (month1 + 12) - month2;
        if (day1 < day2){
            monthInterval--;
        }
        monthInterval %= 12;
        int monthsDiff = Math.abs(yearInterval * 12 + monthInterval);
        if (day1 >= day2) {
            monthsDiff++;
        }
        System.out.println(monthsDiff);
    }
}
